<?php
/*相关配置项 Begin*/
define('MD5_KEY_SH_SHOPPING_BETA', '7467D15BB8DC23FE49C5F6BEC185BF39'); //上海购物节使用的CP接口[测试]
define('MD5_KEY_SH_SHOPPING', 'D0FB5F477B58A0A64A9BF037E3C78324'); //上海购物节使用的CP接口[生产]
define('CHECK_URL_SH_SHOPPING_BETA', 'http://payment-test.chinapay.com/promotion/orderquery.do'); //上海购物节补抽奖数据验证接口[测试]
define('CHECK_URL_SH_SHOPPING', 'http://pay.chinapay.com/promotion/orderquery.do'); //上海购物节补抽奖数据验证接口[生产]
define('SH_SHOPPING_PROMID', 'shopping'); //活动标识[固定值:shopping;]
/*相关配置项 End*/


//引入优惠券发放类
include_once 'lib/SendCouponSimple.php';

/**
 * 上海购物节抽奖相关机能类
 *
 */
class sh_shopping extends ECBaseApp
{
    var $data = array();
    var $backup_path;

    function sh_shopping($params)
    {
        $this->backup_path = SHARE_DATA_PATH . '\data\activities_log/';
        //#--组织相关数据----------
        //将各种参数组装到data对象中
        foreach (array($params) as $_request) {
            foreach ($_request as $_key => $_value) {
                $this->data[$_key] = trim($_value);
            }
        }
    }

    /**
     * 取得上海购物节秒杀商品是否过期
     *
     */
    function get_goods_past()
    {
        $list = array();
        $goods_mod = & m('goods');
        $goods_id_list = explode(",", SH_SHOPPING_MS_GOODS_ID);
        $sql = "
                SELECT goods_id,ad_begin_time,begin_time,end_time  
                FROM " . DB_PREFIX . "store_activities_goods
                WHERE goods_id " . db_create_in($goods_id_list) . "
                AND del_flag = 0
            ";
        $goods_list = $goods_mod->getAll($sql);
        //按照给入的顺序返回商品
        $rst = array();
        $time = gmtime();
        foreach ($goods_id_list as $goods_id) {
            foreach ($goods_list as $goods) {
                if ($goods_id == $goods['goods_id']) {
                    if (empty($rst[$goods_id])) {
                        $tmp = array(
                            "goods_id" => $goods['goods_id'],
                            "past" => ($goods['end_time'] < $time ? 1 : 0),
                        );
                        $rst[$goods_id] = $tmp;
                    } elseif ($rst[$goods_id]['past'] == 1 && $goods['end_time'] > $time) {
                        $rst[$goods_id]['past'] = 0;
                    }
                }
            }
            if (empty($rst[$goods_id])) {
                //非秒杀商品认为活动结束
                $rst[$goods_id] = array(
                    "goods_id" => $goods_id,
                    "past" => 1,
                );
            }
        }
        $result = array();
        //设置键从0开始编号
        foreach ($rst as $goods) {
            $result[] = $goods;
        }

        $result = json_encode($result);
        return $result;
    }

    /**
     * 取得奖品剩余数量
     *
     */
    function get_prize_remain()
    {
        $list = array();
        $goods_mod = & m('goods');
        $prize_goods_id_list = explode(",", SH_SHOPPING_PRIZE_GOODS_ID);
        $sql = "
                SELECT goods_id,sum_stock FROM " . DB_PREFIX . "goods
                WHERE goods_id " . db_create_in($prize_goods_id_list) . "
            ";
        $goods_list = $goods_mod->getAll($sql);

        $rst = json_encode($goods_list);
        return $rst;
    }

    /**
     * 取得中奖用户信息
     *
     */
    function get_user_lottery_list()
    {
        $list = array();
        $sh_shopping_user_lottery_mod = & m('sh_shopping_user_lottery');
        $list = $sh_shopping_user_lottery_mod->get_lottery_list(SH_SHOPPING_SHOW_WINNER_NUM);

        $kannji_list = Lang::get('num_list');
        $kannji_naiyou_list = Lang::get('sh_shopping_lottery');
        $kannji_lottery = Lang::get('lottery_type');
        foreach ($list as $key => $item) {
            switch (intval($item['lottery'])) {
                case 1:
                    $list[$key]['lottery_show'] = sprintf($kannji_lottery, $kannji_list[$item['lottery']]);
                    $list[$key]['lottery_content_show'] = $kannji_naiyou_list[$item['lottery']];
                    break;
                case 2:
                    $list[$key]['lottery_show'] = sprintf($kannji_lottery, $kannji_list[$item['lottery']]);
                    $list[$key]['lottery_content_show'] = $kannji_naiyou_list[$item['lottery']];
                    break;
                case 3:
                    $list[$key]['lottery_show'] = sprintf($kannji_lottery, $kannji_list[$item['lottery']]);
                    $list[$key]['lottery_content_show'] = $kannji_naiyou_list[$item['lottery']];
                    break;
                default:
                    unset($list[$key]);
                    break;
            }
        }
        //dump($list);
        $list = base64_encode(json_encode($list));
        return $list;
    }

    /**
     * 抽奖
     *
     */
    function lottery($user)
    {
        $user_info = $user->get();
        $post_method = $_POST; //$_GET
        $user_id = $user_info['user_id'];
        //处理相关数据，得到结果

        $check_result = $this->_check_info_sh_shopping($this->data, $user_info);
        /*test
        $check_result = array(
            'is_logined' => 1,
            'serial_check' => 1,
        );
        */

        $draw_flag = (isset($_GET["draw"]) && intval($_GET["draw"]) === 0) ? false : true;

        //若验证通过，则进行抽奖
        if ($check_result['is_logined'] === 1
            && $check_result['serial_check'] === 1
            && $draw_flag === true
        ) {
            //默认未中奖
            $check_result['result'] = 0;
            //#--进行抽奖 Begin ----------
            /*
             抽奖方式：
             *、消费金额在 SH_SHOPPING_PRIZE_LIMIT_COST 以下的默认不中奖[孙北京新需求20110908]
             *、已中奖个数超过 SH_SHOPPING_PRIZE_MAX_NUM_PERUSER 时默认不中奖[刘娟新需求20110922]
             1、在当天的奖品配额中随机抽奖
             2、如果中奖的，进行DB保存。保存时需要对奖项进行数量制约，如果不满足的，则认为没有中奖，需要返回最终的中奖结果
             */
            //取得奖品数量.1、取得每日的奖品配额及配置
            $prize_perday_list = json_decode(SH_SHOPPING_PRIZE_PERDAY, true);
            $prize_list = $prize_perday_list;

            //依据规则随机抽奖.获得一个随机数
            $seed = $this->_get_seed(MIN_RANDOM_NUMBER, MAX_RANDOM_NUMBER);
            $lottery_rst = array(
                'rst' => 0, //默认未中奖
            );

            //取得当前用户的已中奖信息
            $user_lottery = & m('sh_shopping_user_lottery');
            $lottery_count_list = $user_lottery->get_lottery_count_list($user_id);
            $summery = 0;
            foreach ($lottery_count_list as $key => $item) {
                $summery += $item['num'];
            }

            //取得当前领奖用户名的已中奖信息
            $user_lottery = & m('sh_shopping_user_lottery');
            $lottery_list = $user_lottery->get_lottery_list_by_user_name_tel($this->data['realName']);
            //取得中奖数量
            $summery_same_user_name_tel = count($lottery_list);

            //当 消费金额少于最低金额限制 时，默认不中奖
            if (intval($this->data['orderAmt']) < SH_SHOPPING_PRIZE_LIMIT_COST) {
            } //当 每个用户的最大中奖次数[0以下表示不限制]有定义， 且 不多于当前用户中奖次数 时，默认不中奖
            elseif ((SH_SHOPPING_PRIZE_MAX_NUM_PERUSER > 0 && SH_SHOPPING_PRIZE_MAX_NUM_PERUSER <= $summery)) {
            } //当 同用户名的最大中奖次数[0以下表示不限制]有定义， 且 使用同样领奖用户名中奖次数不少于该次数 时，默认不中奖
            elseif ((SH_SHOPPING_PRIZE_MAX_NUM_PERUSERNAME > 0 && SH_SHOPPING_PRIZE_MAX_NUM_PERUSERNAME <= $summery_same_user_name_tel)) {
            } else {

                //将中文字符剔除
                $encoding = mb_detect_encoding($this->data['realName'], array('ASCII', 'GB2312', 'GBK', 'UTF-8'));
                $realName_replaced = mb_convert_encoding($this->data['realName'], "utf-8", $encoding);
                $realName_replaced = preg_replace("/[\x{4e00}-\x{9fa5}]+/u", "", $realName_replaced);

                //若用户名不完全是中文的，默认不中奖
                if ($realName_replaced !== "") {
                } //此时才进行实际奖项计算
                else {
                    foreach ($prize_list as $key => $prize) {
                        if ($seed <= $prize['rate']) {
                            $lottery_rst['rst'] = $key;
                            $lottery_rst['coupon_id'] = $prize['c_id'];
                            break;
                        }
                    }
                    //若用户获得的该奖项总数量超过了最大允许数量，则默认不中奖
                    foreach ($lottery_count_list as $key => $item) {
                        if ($item['lottery'] == $lottery_rst['rst']
                            && isset($prize_list[$item['lottery']]['max'])
                            && $item['num'] >= $prize_list[$item['lottery']]['max']
                        ) {
                            //认为不中奖
                            $lottery_rst = array(
                                'rst' => 0,
                            );
                        }
                    }
                }
            }

            //#--进行抽奖 End ------------
            //抽奖成功之后记录相关信息[无论是否中奖]
            $sh_shopping_order_lottery_mod = & m('sh_shopping_order_lottery');
            //记录订单已经进行过抽奖
            $rst = $sh_shopping_order_lottery_mod->add_lottery_times($this->data, $user_id);

            //若有中奖，则尝试记录用户抽奖结果;保存返回中奖，则确认中奖
            if ($lottery_rst['rst'] > 0 && !empty($rst)) {
                $sh_shopping_user_lottery_mod = & m('sh_shopping_user_lottery');
                $data = array(
                    'user_id' => $user_id,
                    'user_name' => $this->data['realName'], //真实姓名[应该需要转码]
                    'tel' => $this->data['tel'], //联系电话
                    'fill_info' => 1, //是否填写了用户信息[因业务调整20110826，该项默认为1:已填写]
                    'ssol_id' => $rst['id'],
                    'lottery' => $lottery_rst['rst'],
                    'lottery_times' => $rst['times'],
                );
                $rst = $sh_shopping_user_lottery_mod->add_user_lottery($data);
                //写入日志
                if (!empty($this->backup_path)) {
                    $this->_write_log(
                        $this->backup_path,
                        'sh_shopping.log',
                        "data:" . print_r($data, true)
                    );
                }

                //若能够保存住该记录，则认可用户中奖
                if (!empty($rst) && !empty($rst['id'])) {
                    $check_result['result'] = $rst['result'];
                    //中奖记录，因业务修改[用户信息不再与抽奖行为分两次处理，而本处废弃]
                    //$check_result['ssul_id'] = $rst['id'];

                    //向用户发放相应的奖品，目前是优惠券
                    $coupon_id = $lottery_rst['coupon_id'];
                    $mobile_code = $this->data['tel'];
                    $template = array(
                        //站内信配置
                        'message_template' => array(
                            'send' => true,
                            'template' => "tobuyer_sh_shopping_lottery_notify",
                        ),
                        //邮件配置
                        'mail_template' => array(
                            'send' => true,
                            'template' => "tobuyer_sh_shopping_lottery_notify",
                        ),
                        //手机短信配置
                        'mobile_message_template' => array(
//TODO:调整为 true
                            'send' => true,
                            'template' => "tobuyer_sh_shopping_lottery_notify",
                        ),
                    );
                    $coupon_send = new SendCouponSimple(
                        array(
                            'coupon_id' => $coupon_id,
                            'user' => $user,
                            'mobile_code' => $mobile_code,
                            'lottery_level' => $check_result['result'],
                        ),
                        $template
                    );
                    $rst = $coupon_send->send_coupon();
                }
            }
        }

        //写入日志
        if (!empty($this->backup_path)) {
            $this->_write_log(
                $this->backup_path,
                'sh_shopping.log',
                "check_result:" . print_r($check_result, true)
            );
            $this->_write_log(
                $this->backup_path,
                'sh_shopping.log',
                "get:" . print_r($_GET, true)
            );
            $this->_write_log(
                $this->backup_path,
                'sh_shopping.log',
                "-----------------------------------------------------------------------------------"
            );
        }
        //#--构造相关XML----------
        $xml = $this->_get_xml_sh_shopping($check_result);
        return $xml;
    }

    /**
     * [废弃]保存用户信息
     * http://1568.chinapay.cn/apicomponent/php/index.php?app=activity&act=sh_shopping&type=fill&real_name=james&id_card=no.1&tel=123456&ssul_id=1
     */
    function save_user_info($user_id)
    {
        //业务改变，该机能废除[20110826 12:16]
        return false;

        //取得展示数据，并以XML对象返回
        $doc = new DOMDocument('1.0', 'utf-8');
        $doc->formatOutput = true;

        $data = array(
            'is_logined' => 1, //默认用户已登录
            'serial_check' => 1, //默认用户订单验证成功
        );

        //抽奖起始时间
        $begin_time = gmstr2time(SH_SHOPPING_BEGIN_TIME);
        //抽奖终了时间
        $end_time = gmstr2time(SH_SHOPPING_END_TIME);
        //当前时间
        $now_time = gmtime();

        //验证用户是否已经登录
        if (empty($user_id)) {
            $data['is_logined'] = 0;
        } //超出活动时间的判断
        elseif ($now_time < $begin_time || $now_time > $end_time) {
            //[-9:活动已结束;0:用户信息不完整;1:用户信息验证通过;]
            $data['serial_check'] = -9;
            //$data['check_msg'] = "无效的活动";
        } //验证用户信息是否完整
        elseif (empty($this->data['real_name'])
            || empty($this->data['id_card'])
            || empty($this->data['tel'])
            || trim($this->data['real_name']) == ""
            || trim($this->data['id_card']) == ""
            || trim($this->data['tel']) == ""
        ) {
            $data['serial_check'] = 0;
            //$data['check_msg'] = "用户信息不完整";
        } //保存用户信息
        else {
            $data['serial_check'] = 1;
            //记录用户信息
            $user_info = array(
                'id' => $this->data['ssul_id'],
                'user_name' => $this->data['real_name'],
                'id_card' => $this->data['id_card'],
                'tel' => $this->data['tel'],
                'fill_info' => 1,
            );
            $sh_shopping_user_lottery_mod = & m('sh_shopping_user_lottery');
            $rst = $sh_shopping_user_lottery_mod->fill_user_info($user_info, $user_id);
            if ($rst === true) {
                $data['save_flag'] = 1;
            } else {
                $data['save_flag'] = 0;
                //$data['check_msg'] = "保存失败";
            }
        }
        $root = $this->_get_xml_body_sh_shopping($data, $doc);
        $doc->appendChild($root); //加入root为根节点

        //设置输出内容的类型为xml
        header('Content-Type: text/xml;');
        return $doc->saveXML();
    }

    /**以下是私有函数***************************************************************************************************/
    /**
     * 验证传递过来的相应参数
     *  return
     *      -9:活动已结束/未开始;
     *      -7:订单付款时间超出了活动时间
     *      -6:订单信息完整性验证通过，但用户信息不全(用于带参访问)[仅限于验证订单信息是否完整，不做CP端请求验证];
     *      -5:请求超时;
     *      -4:商家不能参与抽奖;
     *      -3:订单信息无效(CP端无相应记录);
     *      -2:订单信息已使用(Emall端验证可抽奖次数已满);
     *      -1:订单信息MD5验证失败;
     *       0:订单信息不全;
     *       1:订单信息验证通过;
     */
    private function _check_info_sh_shopping(&$data, $user_info)
    {
        $result = array(
            'is_logined' => 1, //默认用户已登录
            'serial_check' => 1, //默认用户订单验证成功
        );
        $user_id = $user_info['user_id'];

        //验证用户是否已经登录
        if (empty($user_id)) {
            $result['is_logined'] = 0;
            //若未登录，则不继续验证订单信息，直接默认为无效
            $result['serial_check'] = 0;
            //return $result;
        }

        //#--超出活动时间的判断 Begin ----------
        //抽奖起始时间
        $begin_time = gmstr2time(SH_SHOPPING_BEGIN_TIME);
        //抽奖终了时间
        $end_time = gmstr2time(SH_SHOPPING_END_TIME);
        //当前时间
        $now_time = gmtime();
        //若超出了活动时间
        if ($now_time < $begin_time || $now_time > $end_time) {
            //[-9:活动已结束/未开始;]
            $result['serial_check'] = -9;
            return $result;
        }
        //#--超出活动时间的判断 End ------------

        //因为登陆也需要验证活动已结束/未开始,因此,若在此处发现没有登陆的,则返回
        if ($result['is_logined'] === 0) {
            return $result;
        }

        //若是商家，则不继续
        if ($user_info["has_store"] != 0) {
            //-4:商家不能参与抽奖;
            $result['serial_check'] = -4;
            return $result;
        }

//dump($data);
        //#--已登录------------
        //若是由网站引到来的，应该具备以下三个参数，而必须进行数据验证
        if (!empty($data['merId'])
            && !empty($data['transType'])
            && !empty($data['orderId'])
            && !empty($data['orderDate'])
            && !empty($data['mac'])
        ) {
            //组织MD5加密串
            $mac = md5($data['merId'] . $data['orderId'] . $data['orderDate'] . MD5_KEY_SH_SHOPPING);
            //若是调试，则采用调试的MD5Key
            if (SH_SHOPPING_DEBUG) {
                $mac = md5($data['merId'] . $data['orderId'] . $data['orderDate'] . MD5_KEY_SH_SHOPPING_BETA);
            }
//dump($data,$mac);
            //-1:订单信息MD5验证失败
            if ($mac != $data['mac']) {
                $result['serial_check'] = -1;
                return $result;
            } //-6:订单信息验证通过，但用户信息不全(用于带参访问);
            elseif (empty($data['realName'])
                || empty($data['tel'])
            ) {
                $result['serial_check'] = -6;
                return $result;
            }
        }

        //0:订单信息不全
        if (empty($data['orderId'])
            || empty($data['orderDate'])
            || empty($data['orderAmt'])
            || empty($data['realName'])
            || empty($data['tel'])
        ) {
            $result['serial_check'] = 0;
            return $result;
        }

        //上海购物节补抽奖数据验证接口
        $url = CHECK_URL_SH_SHOPPING;
        if (SH_SHOPPING_DEBUG) {
            $url = CHECK_URL_SH_SHOPPING_BETA;
        }
        //组织参数文字串
        $param_str = "promId=" . SH_SHOPPING_PROMID; //活动标识
        !empty($data['orderId']) && $param_str .= (empty($param_str) ? "" : "&") . "orderId=" . $data['orderId']; //订单号，同一笔订单保证此字段值相同
        !empty($data['orderDate']) && $param_str .= (empty($param_str) ? "" : "&") . "orderDate=" . $data['orderDate']; //订单日期,yyyyMMdd（例如：20110401）
        !empty($data['orderAmt']) && $param_str .= (empty($param_str) ? "" : "&") . "orderAmt=" . $data['orderAmt']; //订单金额,单位分，不足12位，左补0（如2元表示为000000000200）
        $mac = md5($data['orderId'] . $data['orderDate'] . $data['orderAmt'] . MD5_KEY_SH_SHOPPING);
        //若是调试，则采用调试的MD5Key
        if (SH_SHOPPING_DEBUG) {
            $mac = md5($data['orderId'] . $data['orderDate'] . $data['orderAmt'] . MD5_KEY_SH_SHOPPING_BETA);
        }
        $param_str .= (empty($param_str) ? "" : "&") . "mac=" . $mac; //MAC，MD5加密orderId+orderDate+orderAmt+MD5Key拼接串
        $rst = $this->_check_order_info_sh_shopping($url, $param_str);
        //-5:请求超时;
        if (empty($rst)) {
            $result['serial_check'] = -5;
            return $result;
        } //-3:订单信息无效(CP端无相应记录);
        elseif ($rst['rst'] === 0) {
            //写入日志
            if (!empty($this->backup_path)) {
                $this->_write_log(
                    $this->backup_path,
                    'sh_shopping.log',
                    "err_msg:" . iconv(
                        'GB2312',
                        "utf-8",
                        $rst['err_msg']
                    ) . " \t orderId:{$data['orderId']} \t orderDate:{$data['orderDate']} \t orderAmt:{$data['orderAmt']}"
                );
            }
            $result['serial_check'] = -3;
            return $result;
        }

        if (!isset($data['merId']) && !empty($rst['info']['merId'])) {
            $data['merId'] = $rst['info']['merId'];
        }
        //使用获得的transType
        if (!empty($rst['info']['transType'])) {
            $data['transType'] = $rst['info']['transType'];
        }

        //-2:订单信息已使用(Emall端验证可抽奖次数已满);
        //取得抽奖次数
        $sh_shopping_order_lottery_mod = & m('sh_shopping_order_lottery');
        $lottery_times = $sh_shopping_order_lottery_mod->get_lottery_times($data);
        //若抽奖次数超过规定次数,则返回-2
        if ($lottery_times >= SH_SHOPPING_LOTTERY_PER_ORDER) {
            $result['serial_check'] = -2;
            return $result;
        }

        //-7:订单付款时间超出了活动时间
        //抽奖起始时间
        $begin_time = gmstr2time(SH_SHOPPING_BEGIN_TIME);
        //抽奖终了时间
        $end_time = gmstr2time(SH_SHOPPING_END_TIME);
        //订单时间
        $pay_time = gmstr2time($data['orderDate']);
        //若超出了活动时间
        if ($pay_time < $begin_time || $pay_time > $end_time) {
            //[-7:订单付款时间超出了活动时间;]
            $result['serial_check'] = -7;
            return $result;
        }

        return $result;
    }

    /**
     * 远端验证用户手填的订单信息
     *
     */
    private function _check_order_info_sh_shopping($url, $param_str)
    {
        $ret = array(); //dump($url."?".$param_str);
        //记录LOG,在本时刻发起HTTP请求
        $this->_write_log(
            $this->backup_path,
            'sh_shopping.log',
            "begin request"
        );

        //发送http请求，获取响应
        $lst = $this->post_request($url, $param_str, SH_SHOPPING_TIMEOUT);
        $rst = $lst['rst'];
        if (empty($rst)) {
            return $ret;
        }
        //依据返回值的特点，将各项拆解倒数组中
        $tmp_array = explode("&", $rst);
        $rst = array();
        foreach ($tmp_array as $item) {
            $tmp = explode("=", $item);
            $rst[$tmp[0]] = $tmp[1];
        }
        $info = $lst['info'];

        //若请求成功
        if ($info['http_code'] == 200) {
            //记录LOG,在本时刻获得HTTP反馈
            $this->_write_log(
                $this->backup_path,
                'sh_shopping.log',
                "get response \t result:" . print_r($rst, true)
            );
            //若CP验证成功
            if ($rst['ResponseCode'] == "1001") {
                $mac = md5($rst['orderId'] . $rst['orderDate'] . $rst['merId'] . MD5_KEY_SH_SHOPPING);
                //若是调试，则采用调试的MD5Key
                if (SH_SHOPPING_DEBUG) {
                    $mac = md5($rst['orderId'] . $rst['orderDate'] . $rst['merId'] . MD5_KEY_SH_SHOPPING_BETA);
                }
                //如果MD5验证成功
                if ($mac == $rst['mac']) {
                    $ret['rst'] = 1;
                    $ret['info'] = array(
                        'orderId' => $rst['orderId'],
                        'orderDate' => $rst['orderDate'],
                        'orderAmt' => $rst['orderAmt'],
                        'merId' => $rst['merId'],
                        'transType' => $rst['transType'],
                        'mac' => $rst['mac'],
                    );
                } else {
                    $ret['rst'] = 0;
                    $ret['err_msg'] = "MD5 check failed!";
                }
            } else {
                $ret['rst'] = 0;
                $ret['err_code'] = $rst['ResponseCode'];
                $ret['err_msg'] = $rst['Message'];
            }
        } else {
            $ret['rst'] = 0;
            $ret['err_msg'] = "http request failed!";
        }
        return $ret;
    }

    /**
     * 取得上海购物节相关的抽奖用XML信息
     *
     */
    private function _get_xml_sh_shopping($data)
    {
        //取得展示数据，并以XML对象返回
        $doc = new DOMDocument('1.0', 'utf-8');
        $doc->formatOutput = true;

        $root = $this->_get_xml_body_sh_shopping($data, $doc);
        $doc->appendChild($root); //加入root为根节点

        //设置输出内容的类型为xml
        header('Content-Type: text/xml;');
        return $doc->saveXML();
    }

    /**
     * 取得上海购物节相关的抽奖用XML信息[明细]
     *
     */
    private function _get_xml_body_sh_shopping($data, $doc)
    {
        $root = $doc->createElement('root'); //新建节点

        foreach ($data as $key => $val) {
            $node = $doc->createElement($key);
            $value = $doc->createTextNode($val);
            $node->appendChild($value);
            $root->appendChild($node);
        }
        return $root;
    }

    /**
     * 写入日志
     *
     */
    function _write_log($path, $file_name, $content)
    {
        if (is_dir($path)) {
            //$now = time() + (8 * 60 * 60);
            $now = time();
            file_put_contents($path . $file_name, date('Y-m-d H:i:s', $now) . "\t{$content}\n", FILE_APPEND);
        }
    }

    /**
     * 取得一个随机数
     *
     */
    function _get_seed($min = 0, $max = 999999)
    {
        return mt_rand($min, $max);
    }
}